package com.Generics.bean.ch03;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

/**
 * @author nmm
 * @date 2024-03-2024/3/30
 * @deriction njm......
 */
public class MyInstanceClazz {

    /**
     * @desc:用于给传递的具体实例进行实例化操作
     * @param myclazz:传递的具体实例
     * @param mymap:(要设置的参数以及参数名)存放与map中
     * @param <T> :泛型
     */
    public <T>  void MyInstance(T myclazz, Map mymap)  {
        Class<?> beanClazz = myclazz.getClass();//获取class实例
        Arrays.stream(beanClazz.getDeclaredFields()).forEach(field -> {
            try {
                String propertyName=field.getName();//属性名
                Class<?> propertyType = field.getType();//参数类型
                String  simpleName=field.getType().getSimpleName();//获取属性具体类型用于数据转化

                String propertyValue = mymap.get(field.getName()).toString();//通过键拿到值
                Object propertyVal=null;//定义全局变量(用于接受转化后的value)

                String setMethodName="set"+propertyName.toUpperCase().charAt(0)+propertyName.substring(1);//拼接set方法名
                Method setMethod = beanClazz.getDeclaredMethod(setMethodName, propertyType);//通过set方法名和参数类型获取具体set方法

                // 根据字段类型进行转换
                if (propertyValue!=null){
                    switch (simpleName) {
                        case "byte":
                        case "Byte":
                            propertyVal = Byte.valueOf(propertyValue);
                            break;
                        case "short":
                        case "Short":
                            propertyVal = Short.valueOf(propertyValue);
                            break;
                        case "int":
                        case "Integer":
                            propertyVal = Integer.valueOf(propertyValue);
                            break;
                        case "long":
                        case "Long":
                            propertyVal = Long.valueOf(propertyValue);
                            break;
                        case "float":
                        case "Float":
                            propertyVal = Float.valueOf(propertyValue);
                            break;
                        case "double":
                        case "Double":
                            propertyVal = Double.valueOf(propertyValue);
                            break;
                        case "boolean":
                        case "Boolean":
                            propertyVal = Boolean.valueOf(propertyValue);
                            break;
                        case "char":
                        case "Character":
                            propertyVal = propertyValue.charAt(0);
                            break;
                        case "String":
                            propertyVal = propertyValue;
                            break;
                    }
                    setMethod.invoke(myclazz,propertyVal);//调用set方法实例化对象
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}